import { theNextDayTime } from "../../../ui-utils/Foundation";
import * as API_Goods from '../../../api/goods'
import * as API_Trade from '../../../api/trade'
import * as API_Order from '../../../api/order'
import * as API_Member from '../../../api/members'
import { md5 } from '../../../lib/md5'

import { formatPrice } from "../../../ui-utils/Foundation";
/* import { timingSafeEqual } from "crypto"; */
// pages/ucenter/my-topUp/my-topUp.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    //金额
    list: [],
    activeId: 0,
    // 支付方式
    //payment: '',
    price: '',
    pay_type: 'wechat',
    shop_id: '',
    password: '',
    balance_status: true,
    show_balance: false,
    focus: false,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({ shop_id: options.shop_id })
    
  },
  updateValue(e) {
    this.setData({ "price": e.detail.value })
  },
  chooseType(e) {
    this.setData({ pay_type: e.currentTarget.dataset.type })
  },
  choosePassword(e) {
    this.setData({ password: e.detail.value })
  },
  submit() {
    if (this.data.price && this.data.price > 0) {
      //const payment_plugin_id = this.data.payment.plugin_id
      if (this.data.pay_type == 'wechat') {
        API_Trade.createShopPay(this.data.shop_id, this.data.price, {
          client_type: 'MINI',
          pay_mode: "normal",
          payment_plugin_id: 'weixinPayPlugin'
        }).then(response => {
          const params = {};
          response.form_items.forEach(item => {
            params[item.item_name] = item.item_value;
          });
          //console.log(params);
          wx.requestPayment({
            ...params,
            success(res) {
              wx.showToast({ title: "支付成功！" });
              setTimeout(() => {
                // wx.switchTab({ url: "/pages/home/home" });
                wx.navigateBack({delta: 2})
              }, 800);
            },
            fail(res) {
              wx.showToast({ title: "用户取消支付", icon: "none" });
            }
          });
        })
      } else if (this.data.pay_type == 'balance') {
        API_Member.seeApyPassword().then(res => {
          this.setData({ balance_status: res, show_balance: true })
          if (res) { this.setData({ focus: true }) }
        })
      }
    } else {
      wx.showToast({
        title: '请输入支付金额且金额不能小于0！',
        image: '/static/images/icon_error.png'
      })
    }
  },

  /* 关闭弹窗 */
  close() {
    this.setData({ show_balance: false, focus: false })
  },
  /* 设置密码 */
  paySetting() {
    wx.navigateTo({
      url: "/ucenter/setting/balance-password/balance-password?type=shop_pay"
    })
  },

  /* 确定支付 */
  payBalance() {
    this.setData({ show_balance: false, focus: false });
    let { password, price, shop_id } = this.data;
    API_Trade.balancePayCash({
      password:md5(password),
      price,
      shop_id
    }).then(res => {
      wx.showToast({ title: '支付成功!' })
      setTimeout(() => {
        wx.navigateBack({
          delta: 2
        })
      }, 800)
    })
    this.setData({
      password:''
    })
  },
  async onShow() {
    /* let item = await API_Trade.getPaymentList('WAP')
    console.log(item, 147)
    this.setData({
      payment: item[1]
    }) */
    // console.log(this.data.payment,111145)
    this.setData({ show_balance: false, focus: false })
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})